package bb.lanxing.util.img;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.transition.Transition;
import bb.lanxing.R;
import bb.lanxing.adapter.BaseListAdapter;
import java.util.List;

public class ImageChooserAdapter extends BaseListAdapter<LocalFile> implements CompoundButton.OnCheckedChangeListener {
    LayoutInflater mInflater;
    OnSelectListener onSelectListener;

    public interface OnSelectListener {
        void onSelectFile(CompoundButton compoundButton, boolean z, LocalFile localFile);
    }

    public ImageChooserAdapter(Context context, List<LocalFile> list) {
        super(context, list);
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View view2;
        final ViewHolder viewHolder;
        int i2 = 0;
        if (view == null) {
            viewHolder = new ViewHolder();
            view2 = this.mInflater.inflate(R.layout.item_image_chooser, viewGroup, false);
            viewHolder.imageView = (ImageView) view2.findViewById(R.id.item_image_choose_img);
            viewHolder.checkBox = (CheckBox) view2.findViewById(R.id.item_image_choose_checkBox);
            viewHolder.ashView = (ImageView) view2.findViewById(R.id.item_image_choose_ash);
            view2.setTag(viewHolder);
        } else {
            view2 = view;
            viewHolder = (ViewHolder) view.getTag();
        }
        LocalFile localFile = (LocalFile) this.dataSet.get(i);

        CustomTarget<Drawable> ct = new CustomTarget<Drawable>() {
            @Override
            public void onLoadCleared(@Nullable Drawable drawable) {
            }

//            @Override
//            public void onResourceReady(Object obj, Transition transition) {
//                onResourceReady((Drawable) obj, (Transition<? super Drawable>) transition);
//            }


            @Override
            public void onLoadStarted(Drawable drawable) {
                super.onLoadStarted(drawable);
            }

            @Override
            public void onLoadFailed(Drawable drawable) {
                super.onLoadFailed(drawable);
            }

            @Override
            public void onResourceReady(@NonNull Drawable drawable, @Nullable Transition<? super Drawable> transition) {
                viewHolder.imageView.setImageDrawable(drawable);
                try {
                    viewHolder.imageView.getDrawable().setColorFilter(Color.argb(255, 238, 238, 238), PorterDuff.Mode.MULTIPLY);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

//todo
//        RequestBuilder<Drawable> rb = (RequestBuilder<Drawable>)(ct);

        String localImage = localFile.getUri().toString();
//        DrawableRequestBuilder
        Context ctx = view2.getContext();
        Glide.with(ctx).load(localImage).override(100,100).into(ct);



        boolean contains = LocalImageHelper.getInstance().getCheckedItems().contains(localFile);
        viewHolder.checkBox.setOnCheckedChangeListener(null);
        viewHolder.checkBox.setChecked(contains);
        ImageView imageView = viewHolder.ashView;
        if (!contains) {
            i2 = View.GONE;
        }
        imageView.setVisibility(i2);
        viewHolder.checkBox.setTag(i);
        viewHolder.checkBox.setTag(R.id.item_image_choose_ash, viewHolder.ashView);
        viewHolder.checkBox.setOnCheckedChangeListener(this);
        return view2;
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean z) {
        int intValue = (Integer) compoundButton.getTag();
        View view = (View) compoundButton.getTag(R.id.item_image_choose_ash);
        LocalFile localFile = (LocalFile) this.dataSet.get(intValue);
        view.setVisibility(z ? View.VISIBLE : View.GONE);
        if (this.onSelectListener != null) {
            compoundButton.setOnCheckedChangeListener(null);
            this.onSelectListener.onSelectFile(compoundButton, z, localFile);
            compoundButton.setOnCheckedChangeListener(this);
        }
    }


    public class ViewHolder {
        ImageView ashView;
        CheckBox checkBox;
        ImageView imageView;

        ViewHolder() {
        }
    }

    public OnSelectListener getOnSelectListener() {
        return this.onSelectListener;
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }
}
